package org.dtrd.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.food.entity.bean.CookbookCategoryDetail;
import org.dtrd.modules.food.entity.bean.CookbookCategoryListInfo;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwCookbookCategory;
import org.dtrd.modules.food.entity.request.CookbookCategoryListRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwCookbookCategoryMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwCookbookCategoryService;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 菜谱分类 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-09
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwCookbookCategoryServiceImpl extends ServiceImpl<DtrdExtEmsYwCookbookCategoryMapper, DtrdExtEmsYwCookbookCategory> implements IDtrdExtEmsYwCookbookCategoryService {

    private DtrdExtEmsYwCookbookCategoryMapper dtrdExtEmsYwCookbookCategoryMapper;

    @Override
    public List<CookbookCategoryListInfo> getCookbookCategoryList(CookbookCategoryListRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwCookbookCategory> queryWrapper = baseQueryWrapper();
        String categoryName = request.getCategoryName();
        if (StrUtil.isNotBlank(categoryName)) {
            queryWrapper.like(DtrdExtEmsYwCookbookCategory::getCategoryName, StringUtil.fuzzySearchWrapper(categoryName));
        }
        List<DtrdExtEmsYwCookbookCategory> poList = list(queryWrapper);
        List<CookbookCategoryListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(CookbookCategoryListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public CookbookCategoryDetail getCookbookCategory(Integer dataId) {
        DtrdExtEmsYwCookbookCategory dtrdExtEmsYwCookbookCategory = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwCookbookCategory)
                        .map(po -> new CookbookCategoryDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateCookbookCategory(CookbookCategoryDetail dto) {
        DtrdExtEmsYwCookbookCategory dtrdExtEmsYwCookbookCategory = new DtrdExtEmsYwCookbookCategory();
        if(dto.getDataId() == null){
            dtrdExtEmsYwCookbookCategory = dtrdExtEmsYwCookbookCategory.init();
        }
        dtrdExtEmsYwCookbookCategory.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwCookbookCategory);
    }

    @Override
    public boolean removeCookbookCategory(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwCookbookCategory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwCookbookCategory.class)
                        .eq(DtrdExtEmsYwCookbookCategory::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwCookbookCategory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwCookbookCategory.class)
                        .eq(DtrdExtEmsYwCookbookCategory::getIsDel, 0);
    }
}
